package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Api("通用接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/admin/common/upload")
    public Result<String> uploadPic(MultipartFile file) throws IOException {
        log.info("文件上传z:{}",file);
        //后缀名
        String substring = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //防止重复
        String uuid = UUID.randomUUID().toString();
        //文件新名字
        String name = "苍穹/" + uuid + substring;
        //请求路径
        String filePath = aliOssUtil.upload(file.getBytes(), name);
        return Result.success(filePath);
    }

    @GetMapping("/courier/validateCodeToPhone")
    public Result sendMessageToPhone(String telephone){
        //1.短信发送
        String code = "";
        //生成随机6为数字得验证🐎
        Random random = new Random();
        for(int i=0;i<4;i++){
            code+=random.nextInt(10);
        }
        //短息发送
        // alismsTemplate.sendMessage(telephone,code);
        //2.保存到redis
        String key = "code:"+telephone;
        log.info("验证码为：{}",code);
        redisTemplate.opsForValue().set(key,code,600, TimeUnit.SECONDS);
        return Result.success(code);
    }
}
